<?php
/**
 * @author Nguyen Xuan Hong
 * @copyright 2011
 */
if (!class_exists('clsDbTable'))
	exit;

class clsAdminCPTable extends clsDbTable {
	
	var $session_name = "acpsession";
	var $session_dvalue = "021x3294e";
	
	function confirmSession(){
		if($_SESSION[$this->session_name] == $this->session_dvalue && $_SESSION[$this->session_name]!='')
			return true;
		return false;
	}
	
	function setSession(){
		$_SESSION[$this->session_name] = $this->session_dvalue;
	}
	
	
	function doLogOut(){
		$_SESSION[$this->session_name] = '';
		session_destroy();
	}
	
	// Print loginform to screen
	function putLoginForm($login_fall_flag = false){
		if(!class_exists('clsForm'))
			require_once('form.php');
		
		$loginform	= new clsForm( array(
					'username'	=>	array(
						'title'		=>	'Username',
						'type'		=>	'text',
						'length'	=>	50,
						'null'		=>	false,
						'desc'		=>	'Tên đăng nhập'
						),
					'password'	=>	array(
						'title'		=>	'Password',
						'type'		=>	'password',
						'lenght'	=>	50,
						'null'		=>	false,
						'desc'		=>	'Mật khẩu'
						)
					));
		
		if($login_fall_flag)
			$loginform->setErrorString("Lỗi đăng nhập không đúng tên đăng nhập hoặc mật khẩu / Invaild Username or Password");
		
		$eccode = '
				<script>
				$(function() {
				$("#loginform").draggable({ handle: "td" });
				});
				</script>
				<div align="center">';	
		
		$eccode .= $loginform->createForm("Đăng nhập","loginform",'index.php?mode=login',array('submit'=>'Đăng nhập','style'=>'width:400px; top:50px;','tableid'=>'loginform'));
		
		return  $eccode ."</div>" ;
	}
	
	function doLogin($username,$password){
		
		require_once('admin.tbl.php');
		$pClsAdmin = new clsAdminTable();
		//return $pClsAdmin->login($username,$password);
		if($pClsAdmin->login($username,$password)) {	
			$this->setSession();
			return true;
		}
		else{
			$this->doLogOut();
			return false;
		}
		//die( ($pClsAdmin->login($username,$password))?'1':'2');
	}
	
	var $array_menu = array();
	
	function putHeadFrame(){
		return "<div align='right'><a style='cursor:pointer'  onclick='window.parent.location.href=\"?logout\"'><strong>Logout</strong></a></div>";
	}
	
	function putMainFrame($page){
		// Filter page
		//if(!preg_match ("^([a-z0-9_-]*)$",$page)) return "kth";
		
		$file = $page.'.php';
		if(file_exists($file))
			require_once($file);
		else
			return $this->putError(array(
						'page'	=>	'Administrator Control Panel - Framework',
						'msg'	=>	'Could not founded file '.$page.'.php - Check avaible in admincp folder',				
						),'html');
	}
	
	function putLeftFrame($arr_menu){
		/*$putstr = '
		<link rel="stylesheet" type="text/css" href="css/left.css"/>
		<script>
			$(document).ready(function(){
				$(".eleftmenu-element").slideUp("fast");
				
				$(".eleftmenu").click(function(){
					var idvar = $(this).attr("menuid");
					$("[childmenuid="+idvar+"]").slideToggle(200);
				});
			});
		</script>
		<table cellpadding="5" cellspacing="5" width="100%" class="" style="margin-bottom:5">';
		$id = 0;
		foreach ($arr_menu as $key => $arr) {	
			$putstr .= '
				<tr>
					<td menuid="'.$id.'" class ="ui-dialog-titlebar ui-widget-header ui-corner-all eleftmenu" height = "15px" style = "-moz-border-radius:5; cursor:pointer" >
						<div>
							<b>'.$key.'</b>
						</div>
					</td>
				</tr>
				<tr>
				<td>
					<div class="eleftmenu-element" childmenuid="'.$id.'"  >
						<table width="100%" cellpadding = "5" cellspacing = "5">';
			$id++;
			foreach ($arr as $x_key => $x_val) {
				$putstr .=  '<tr>
						<td class="text ui-widget-content ui-corner-all" height = "20px">
							<a href="?p='.$x_val['page'].'&mode='.$x_val['mode'].'">'.$x_key.'</a>
						</td>
					  </tr>';
			}
			$putstr .=  '</table>
					</div>
				</td>
				</tr>	
					';		
		}
		$putstr .=  '</table>';
		return $putstr;*/
		
		
		$putstr = '
				<div id="accordionsss" >';
		/*if(!$pClsAdmin){
			//$arrstr = explode(',',$str);
			//FB::info($arrstr);
			foreach ($arr_menu as $key => $arr) {	
				$putstr .= '
				<h3><a >'.$key.'</a></h3><div>';
				foreach ($arr as $x_key => $x_val) {
					//$check = $x_val['mode'] . $x_val['page'];
					//foreach($arrstr as $z_key => $z_val){
						//if($check == $z_val){
							//FB::info($z_val);
							//$check1 = $x_val['mode'] . $x_val['page'];
							//FB::info($check1);
							$putstr .=  '
								<p>
									<a href="?p='.$x_val['page'].'&mode='.$x_val['mode'].'">'.$x_key.'</a>
								</p>
							';
						//}
					//}
				}
				//FB::info($check1);
				foreach($arrstr as $y_key => $y_val){
					if($check1 == $y_val){
						//FB::info($check1);
						$putstr .= $head . $body . '</div>';
						$body='';
					}
				}
			}
		}else {*/
		//FB::info($arr_menu);
		foreach ($arr_menu as $key => $arr) {	
			$putstr .= '
					<h3><a >'.$key.'</a></h3><div>';
			foreach ($arr as $x_key => $x_val) {
				$putstr .=  '
						<p>
						<a href="?p='.$x_val['page'].'&mode='.$x_val['mode'].'">'.$x_key.'</a>
						</p>
						';
			}
			$putstr .= '</div>';
		}
		//}
		$putstr .= '</div>';
		$putstr .= '
				<script>
				$(function() {
				//	   alert("SA");
				$("#accordionsss").accordion({autoHeight: false,navigation: true});
				}); </script>';	
		
		return $putstr;
	}
}
?>